package com.dhcc.sds.standard.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sds.standard.dao.SdsStandDataContrastLogMapper;
import com.dhcc.sds.standard.dao.SdsStandDataContrastMapper;
import com.dhcc.sds.standard.entity.SdsStandDataContrast;
import com.dhcc.sds.standard.entity.SdsStandDataContrastLogEntity;
import com.dhcc.sds.standard.service.ISdsStandDataContrastService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;


@Service("sdsStandDataContrastService")
public class SdsStandDataContrastServiceImpl extends BaseServiceImpl<SdsStandDataContrastMapper, SdsStandDataContrast> implements  ISdsStandDataContrastService {

    @Autowired
    SdsStandDataContrastMapper sdsStandDataContrastMapper;

    @Autowired
    SdsStandDataContrastLogMapper standDataContrastLogMapper;

    @Override
    public Map<String, List<Long>> loadContrastByDataId(Long dataId) {
        Wrapper<SdsStandDataContrast> rrw = new EntityWrapper<>();
        rrw.eq("data_id", dataId);
        Map<String, List<Long>> resMap = new HashMap<>();
        List<Long> dicIDs = new ArrayList<>();
        List<SdsStandDataContrast> contrasts = this.sdsStandDataContrastMapper.selectList(rrw);
        if(CommonUtil.isNotEmpty(contrasts)){
            for (SdsStandDataContrast rr : contrasts) {
                dicIDs.add(rr.getDicId());
            }
        }
        resMap.put("dic", dicIDs);
        return resMap;
    }

    @Override
    public Boolean contrast(Long dataId, Long dicId, Boolean isAdd) {
        SdsStandDataContrast entity = new SdsStandDataContrast();
        SdsStandDataContrastLogEntity logEntity = new SdsStandDataContrastLogEntity();
        entity.setDataId(dataId);
        entity.setDicId(dicId);
        entity.setOperDatetime(new Date());
        SdsStandDataContrast rr = this.sdsStandDataContrastMapper.selectOneById(entity.getDataId(),entity.getDicId());
        if(isAdd){
            if(CommonUtil.isNotEmpty(rr)){
                return false;
            }
            entity.insert();
            logEntity.setDataId(entity.getDataId());
            logEntity.setDicId(entity.getDicId());
            logEntity.setOperaType(1);
            logEntity.setOperaTime(entity.getOperDatetime());
            User user = ShiroKit.getUser();
            logEntity.setOperaName(user.getLoginName());
            logEntity.insert();
        }else{
            if(CommonUtil.isNotEmpty(rr)){
                SdsStandDataContrastLogEntity up = new SdsStandDataContrastLogEntity();
                up.setDataId(dataId);
                up.setDicId(dicId);
                up.setOperaType(0);
                up.setOperaTime(new Date());
                User user = ShiroKit.getUser();
                up.setOperaName(user.getLoginName());
                up.insert();
                rr.deleteById();
                return false;
            }
        }
        return true;
    }
}
